package com.yh.workbill.ui.companyInfo.mvp;

import com.yh.workbill.model.Company;
import com.yh.workbill.model.WorkBill;
import com.yh.workbill.ui.companyInfo.CompanyListInfoViewModel;

import java.util.List;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class AddCompanyInfo_Presenter implements LifecycleObserver {
    private AddCompanyInfo_View view;
    private AddCompanyInfo_Model model;

    public AddCompanyInfo_Presenter(AddCompanyInfo_View view) {
        this.view = view;
        this.model = new AddCompanyInfo_Model();
    }

    public void getQueryData(CompanyListInfoViewModel viewModel, boolean isRefresh, boolean isLoadMore, int pageindex, int pagenumber) {
        view.showDialog("正在查询数据", true);
        model.getData(pageindex, pagenumber, new AddCompanyInfo_Model.CompanyDataCallBack() {
            @Override
            public void onSuccess(List<Company> data) {
                view.hideDialog();
                Company[] workBills = data.toArray(new Company[data.size()]);
                if (isRefresh) {
                    if (viewModel.getCompanydatas().getValue() != null) {
                        viewModel.getCompanydatas().getValue().clear();
                    }
                    viewModel.addCompanydatas(0, workBills);
                    view.refresh();
                } else if (isLoadMore) {
                    int index = viewModel.getCompanydatas().getValue().size();
                    viewModel.addCompanydatas(index, workBills);
                    view.loadMore();
                }
                view.getData(data);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.queryFail(msg);
            }
        });
    }

    public void addCompany(String company_name, String company_address, String company_phone) {
        view.showDialog("正在添加公司", false);
        model.addCompanyInfo(company_name, company_address, company_phone, new AddCompanyInfo_Model.AddCallBack() {
            @Override
            public void onSuccess(String company, String company_address, String company_phone) {
                view.hideDialog();
                view.addSuccess(company, company_address, company_phone);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.addFail(msg);
            }
        });
    }


    public void delCompany(String company_name, String uuid) {
        view.showDialog("正在删除 " + company_name + " 信息", true);
        model.delCompanyInfo(company_name, uuid, new AddCompanyInfo_Model.DelCallBacl() {
            @Override
            public void onSuceess(String company) {
                view.hideDialog();
                view.delSuccess(company);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.addFail(msg);
            }
        });
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestory() {
        if (this.view != null) {
            this.view = null;
        }
    }
}
